using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace NET.Library.RegularExpressions
{
    /// <summary>
    /// Regex Object which provides some useful Regular Expressions.
    /// </summary>
    public class Regex
    {
        #region Constants
        private const string _UKPhoneNumber = @"^\d{5} \d{6}$";
        private const string _UKLondonPhoneNumber = @"^\d{4} \d{3} \d{4}$";
        private const string _NationalInsuranceNumber = "^[A-CEGHJ-PR-TW-Z]{1}[A-CEGHJ-NPR-TW-Z]{1}[0-9]{6}[A-DFM]{0,1}$";//@"^\w{2} \d{2} \d{2} \d{2} \w{1}$";
        private const string _UKDate = @"(((0[1-9]|[12][0-9]|3[01])([/])(0[13578]|10|12)([/])(\d{4}))|(([0][1-9]|[12][0-9]|30)([/])(0[469]|11)([/])(\d{4}))|((0[1-9]|1[0-9]|2[0-8])([/])(02)([/])(\d{4}))|((29)(/)(02)([/])([02468][048]00))|((29)([/])(02)([/])([13579][26]00))|((29)([/])(02)([/])([0-9][0-9][0][48]))|((29)([/])(02)([/])([0-9][0-9][2468][048]))|((29)([/])(02)([/])([0-9][0-9][13579][26])))";
        private const string _UKLongDate = "^((0[1-9])|(1[0-9])|(2[0-9])|(3[01]))\\s(January|March|May|July|August|October|December)\\s([0-9]{4})|((0[1-9])|(1[0-9])|(2[0-9])|(30))\\s(April|June|September|November)\\s([0-9]{4})|((0[1-9]|1[0-9]|2[0-8])\\s(February)\\s([0-9]{4}))|((29)\\s(February)\\s([02468][048]00))|((29)\\s(February)\\s([13579][26]00))|((29)\\s(February)\\s([0-9][0-9][0][48]))|((29)\\s(February)\\s([0-9][0-9][2468][048])|((29)\\s(February)\\s([0-9][0-9][13579][26])))$";
        private const string _Email = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
        #endregion

        #region Properties
        /// '-----------------------------------------------------------------------------------------
        /// <summary>
        /// UKPhoneNumber expression
        /// </summary>
        /// <returns></returns>
        /// '-----------------------------------------------------------------------------------------
        public static string UKPhoneNumber
        {
            get { return _UKPhoneNumber; }
        }

        /// '-----------------------------------------------------------------------------------------
        /// <summary>
        /// UKLondonPhoneNumber expression
        /// </summary>
        /// '-----------------------------------------------------------------------------------------
        public static string UKLondonPhoneNumber
        {
            get { return _UKLondonPhoneNumber; }
        }

        /// '-----------------------------------------------------------------------------------------
        /// <summary>
        /// NationalInsuranceNumber expression
        /// </summary>
        /// '-----------------------------------------------------------------------------------------
        public static string NationalInsuranceNumber
        {
            get { return _NationalInsuranceNumber; }
        }

        /// '-----------------------------------------------------------------------------------------
        /// <summary>
        /// UKDate expression
        /// </summary>
        /// '-----------------------------------------------------------------------------------------
        public static string UKDate
        {
            get { return _UKDate; }
        }

        /// '-----------------------------------------------------------------------------------------
        /// <summary>
        /// UKLongDate expression
        /// </summary>
        /// '-----------------------------------------------------------------------------------------
        public static string UKLongDate
        {
            get { return _UKLongDate; }
        }

        /// '-----------------------------------------------------------------------------------------
        /// <summary>
        /// 
        /// </summary>
        /// '-----------------------------------------------------------------------------------------
        public static string Email
        {
            get { return _Email; }
        }
        #endregion

        #region Static Methods
        /// <summary>
        /// 
        /// </summary>
        /// <param name="phoneNumber"></param>
        /// <returns></returns>
        /// <example>01276 507763</example>
        public static bool IsUKPhoneNumber(string phoneNumber)
        {
            return IsMatch(phoneNumber, _UKPhoneNumber);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="phoneNumber"></param>
        /// <returns></returns>
        /// <example>0207 386 8128</example>
        public static bool IsUKLondonPhoneNumber(string phoneNumber)
        {
            return IsMatch(phoneNumber, _UKLondonPhoneNumber);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        /// <example>GW 18 13 56 T</example>
        public static bool IsNationalInsuranceNumber(string number)
        {
            return IsMatch(number, _NationalInsuranceNumber);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static bool IsUKDate(string date)
        {
            return IsMatch(date, _UKDate);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static bool IsUKLongDate(string date)
        {
            return IsMatch(date, _UKLongDate);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public static bool IsEmail(string email)
        {
            return IsMatch(email, _Email);
        }
        #endregion

        #region Private Methods
        private static bool IsMatch(string value, string pattern)
        {
            bool match = false;

            if (System.Text.RegularExpressions.Regex.IsMatch(value, pattern))
            {
                match = true;
            }

            return match;
        }
        #endregion
    }
}
